package com.sen.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sen.entity.DruginfoEntity;
import com.sen.entity.EmpEntity;
import com.sen.entity.EmpRoleEntity;
import com.sen.service.EmpRoleService;
import com.sen.service.EmpService;
import com.sen.utils.GetUserName;
import com.sen.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 职员表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2021-01-08 17:54:40
 */
@RestController
@RequestMapping("emp")
public class EmpController {
    @Autowired
    private EmpService empService;
    @Autowired
    private EmpRoleService empRoleService;

    /**
     * 通过分页查询所有员工
     * @param emp 所有员工类相关信息
     * @return
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String,String> emp){
        Page<EmpEntity> page = new Page<>(Integer.parseInt(emp.get("current")),Integer.parseInt(emp.get("size")));
        IPage<EmpEntity> empList = empService.queryAll(page,emp);
        return R.ok().put("empList", empList);
    }

    /**
     *  通过员工id查询员工信息
     *
     * @param eid 员工id
     * @return
     */
    @RequestMapping("/queryEmpById")
    public R queryEmpById(String eid) {
        EmpEntity emp = empService.getOne(new QueryWrapper<EmpEntity>().eq("eid", eid));
        EmpRoleEntity roleId = empRoleService.getOne(new QueryWrapper<EmpRoleEntity>().eq("eid", eid));
        emp.setRoleId(roleId.getRoleid());
        return R.ok().put("emp",emp);
    }
    /**
     *  修改员工信息
     *
     * @param emp 员工实体类
     * @return
     */
    @RequestMapping("/updEmpInfo")
    public void updEmpInfo(EmpEntity emp) {
        //修改用户信息
        empService.update(emp, new UpdateWrapper<EmpEntity>().eq("eid", emp.getEid()));
        //修改用户角色
        empRoleService.update(new UpdateWrapper<EmpRoleEntity>().eq("eid", emp.getEid()).set("roleid", emp.getRoleId()));
    }
    /**
     *  修改个人信息
     *
     * @param emp 员工实体类
     * @return
     */
    @RequestMapping("/updMyInfo")
    public void updMyInfo(EmpEntity emp) {
        //修改用户信息
        empService.update(emp, new UpdateWrapper<EmpEntity>().eq("eid", emp.getEid()));
    }
    /**
     *  修改个人信息
     *
     * @param username 员工登录名
     * @return
     */
    @RequestMapping("/userNmaeOnly")
    public R userNmaeOnly(String username) {
        EmpEntity emp = empService.getOne(new QueryWrapper<EmpEntity>().eq("username", username));
        if (emp!=null){
            return R.error();
        }else{
            return R.ok();
        }
    }
    /**
     * 通过分页查询所有员工
     * @return
     */
    @RequestMapping("/getUserName")
    public R getUserName(){
        String username = GetUserName.getStringRandom();
        return R.ok().put("username",username);
    }
    /**
     * 员工复职离职
     * @return
     */
    @RequestMapping("/empLever")
    public void empLever(String eid,String state){
        empService.update(new UpdateWrapper<EmpEntity>().eq("eid",eid).set("state",state));
    }

    /**
     * 添加员工
     * @return
     */
    @RequestMapping("/addEmp")
    public R addEmp(EmpEntity empEntity,Integer roleId){
        empService.addEmp(empEntity,roleId);
        return R.ok();
    }
    /**
     * 确认原密码密码
     * @return
     */
    @RequestMapping("/oldPwd")
    public R oldPwd(String oldPwd,String nowPwd){
        //如果当前登录用户密码和要修改密码时输入相同返回code:200
        if (new BCryptPasswordEncoder().matches(oldPwd, nowPwd)) {
            return R.ok();
        }
        else{
            return R.error();
        }
    }

    /**
     * 确认原密码密码
     *
     * @return
     */
    @RequestMapping("/updPwd")
    public R updPwd(Integer id, String newPwd,String oldPwd) {
        try{
            if (new BCryptPasswordEncoder().matches(newPwd,oldPwd)) {
                return R.error(411, "新密码不能与旧密码重复");
            }
            else{
                empService.update(new UpdateWrapper<EmpEntity>().eq("eid", id).set("password", new BCryptPasswordEncoder().encode(newPwd)));
                EmpEntity emp = empService.getById(id);
                return R.ok().put("emp",emp);
            }
        }
        catch (Exception e){
            return R.error("未知异常请稍后重试");
        }
    }
}
